//
//  ReasonPhotoView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/3.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol ReasonPhotoDelegate:class {
    func selectPhoto()
}
class ReasonPhotoView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    var backView:UIView?
    weak var delegate:ReasonPhotoDelegate?
    let BtnTag = 1000
    var photoArr:Array = [UIImage]()
    var collectionView:UICollectionView?
    lazy var textView:BaseTextView = {
        let textView = BaseTextView(changeView: backView!)
        textView.font = UIFont.systemFont(ofSize: 12)
        textView.themeBackground = ThemeManager.ThemeColorKeys.background2
        return textView
    }()
    
    lazy var placeHolderLabel:UILabel = {
        let label = BaseLabel(style: .thirdStyle)
        label.text = kText("请描述您申诉详情，我们会及时跟进并解决。\n（请上传相关问题的截图）")
        label.numberOfLines = 0
        label.sizeToFit()
        
        return label
    }()
    
    init(dataSource:UIView) {
        super.init(frame: CGRect.zero)
        backView = dataSource
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        self.addSubview(textView)
//        textView.delegate = self
        textView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.width.equalTo(kSCREEN_WIDTH-32)
            make.top.equalToSuperview().offset(10)
            make.height.equalTo(196)
        }
        
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.textColor = UIColor(hexStr:"#c0c0c0")
        textView.font = UIFont.systemFont(ofSize: 13)
        placeHolderLabel.font = UIFont.systemFont(ofSize: 12)
        textView.setValue(placeHolderLabel, forKey: "_placeholderLabel")
        
        
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16)
        layout.itemSize = CGSize(width: 100, height: 100)
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 12
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        self.collectionView = collectionView
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.themeBackground = ThemeManager.ThemeColorKeys.background
        collectionView.register(BaseCollectionViewCell.self, forCellWithReuseIdentifier: "identifier")
        self.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(156)
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if photoArr.count >= 3 {
            return 3
        }
        return photoArr.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:BaseCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath) as! BaseCollectionViewCell
        cell.contentView.subviews.forEach { (view) in
            view.removeFromSuperview()
        }
        let backImageView = UIImageView()
        backImageView.isUserInteractionEnabled = true
        cell.contentView.addSubview(backImageView)
        backImageView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        let removeBtn = UIButton()
        removeBtn.tag = indexPath.row + BtnTag
        removeBtn.addTarget(self, action: #selector(removeBtnAction(sender:)), for: UIControlEvents.touchUpInside)
        removeBtn.setBackgroundImage(UIImage(named: "appeal_delete"), for: UIControlState.normal)
        backImageView.addSubview(removeBtn)
        removeBtn.snp.makeConstraints { (make) in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(25)
        }
        
        
        if indexPath.row == photoArr.count && photoArr.count != 3 {
            backImageView.themeImage = ThemeManager.ThemeImageKeys.appeal
            removeBtn.isHidden = true
        }else {
            backImageView.image = photoArr[indexPath.row]
            removeBtn.isHidden = false
        }
        return cell
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == photoArr.count && photoArr.count != 3 {
            if delegate != nil {
                self.delegate?.selectPhoto()
            }
        }
    }
    func changePhoto() {
        collectionView?.reloadData()
    }
    
    @objc func removeBtnAction(sender:UIButton) {
        photoArr.remove(at: sender.tag - BtnTag)
        collectionView?.reloadData()
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
